/**
 * Copyright (c) 2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

var Toolbar = {};

Toolbar.Base = new Class(
{
	initialize: function(attachTo, options)
	{
		this.buttons = this.buttons || [];
		this.options = Object.extend({
			className: 'toolbar',
			insert:    'Before'
		}, options || {});
		
		// creation
		this.create();
		
		// events
		this.toolbar.addEvent('click', this.action.bindWithEvent(this));
	},
	
	create: function()
	{
		this.toolbar = new Element('span', {'class': this.options.className});
		
		// buttons
		this.buttons.forEach(function(bt)
		{
			if (bt.length > 0) {
				this.addButton(bt[0], bt[1]);
			}
			else {
				this.addSeparator();
			}
		}, this);
		
		// injects in DOM
		this.toolbar['inject' + this.options.insert](attachTo);
	},
	
	addButton: function(className, title, content, style)
	{
		var a  = new Element('a', {
			'href':  '#',
			'class': className,
			'title': title
		});
		var el = new Element('span').injectInside(a);
		
		if (style) {
			el.setStyles(style);
		}
		if (typeof content == 'string') {
			el.innerHTML = content;
		}
		else {
			el.appendChild(content);
		}
		this.toolbar.appendChild(a);
	},
	
	addSeparator: function()
	{
		this.toolbar.appendChild(new Element('span', {'class': 'separator'}));
	},
	
	action: function(evt)
	{
		evt.stop();
		var el = evt.target;
		while(el.get('tag') != 'a')
		{
			if (el.hasClass('toolbar')) {
				return;
			}
			el = el.parentNode;
		}
		this['cb_' + el.className]();
	}
});
